<?php
class ArticleAction extends BaseAction {
	/**
	 * 信息列表
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $article = new ArticleViewModel();
	 * $list = $article->select();
	 * [/example]
	 *
	 */
	function index() {
		$article = new ArticleViewModel();
		$list = $article->select();
		$this->assign ( 'ulist', $list );
		$this->display();
	
		//dump($list);
	}
	
	/**
	 * 添加信息页面
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $article = new ArticleViewModel();
	 * $user->init();
	 * $userInfo = $user->getUserInfoByUid($uid);  * 
	 * [/example]
	 *
	 */
	function add() {
		//获取单元列表
		$section = new SectionModel();
		$list = $section->order(id)->select();
		$this->assign("slist", $list);
			
			
		//获取分类列表
		$category = new CategoryModel();
		$list = $category->order(id)->select();
		$this->assign("clist", $list);
		
		//获取分类属性
		$sysState = new SysStateModel();
		$where['table_name'] = array('eq', 'joys_article');
		$where['col_name'] = array('eq', 'attribute');
		$list = $sysState->where($where)->order('order_num')->select();
		$this->assign("attrlist", $list);
		$this->display();
	}
	
	/**
	 * 编辑页面
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $art = new ArticleModel();
	 * $art->where ( 'id in (' . $artId . ')' )->delete();
	 * [/example]
	 *
	 */
	function edit() {
		//$id = $_POST['id'];
		//if(!empty($id) && is_array($id)){
		$id = $_GET['id'];
		//dump($id);
		if(!empty($id)){
			$artId = $id; //implode ('', $id );	//数组转换成字符串
			//dump($artId);
			$article = new ArticleModel();
			$data = $article->getById($artId);
			$this->assign("article", $data);
			
			//获取单元列表
			$section = new SectionModel();
			$list = $section->order(id)->select();
			$this->assign("slist", $list);
			//dump($list);
				
			//获取分类列表
			$category = new CategoryModel();
			$list = $category->order(id)->select();
			$this->assign("clist", $list);
		
			//获取分类属性
			$sysState = new SysStateModel();
			$where['table_name'] = array('eq', 'joys_article');
			$where['col_name'] = array('eq', 'attribute');
			$list = $sysState->where($where)->order('order_num')->select();
			$this->assign("attrlist", $list);
			
		}else{
			echo '请选择要编辑的信息！';
		}
		$this->display();
	}
	
	/**
	 * 删除信息
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $art = new ArticleModel();
	 * $art->where ( 'id in (' . $artId . ')' )->delete();
	 * [/example]
	 *
	 */
	function delete() {
		$id = $_POST['id'];
		
		//dump ( $id );
		if (! empty ( $id ) && is_array ( $id )) {
			$art = new ArticleModel();
			$artId = implode ( ',', $id );
			$flag = $art->where ( 'id in (' . $artId . ')' )->delete();
			if ($flag) {
				//$this->assign('jumpUrl',__URL__.'/index');
				//$this->success('操作成功');
				echo "删除信息成功!";
			} else {
				echo $art->getDbError();
			
		//$this->error('操作失败：'.$art->getDbError());
			}
		} else {
			echo '请选择删除的信息！';
		}
	
	}
	
	/**
	 * 添加信息
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $article = new ArticleViewModel();
	 * $user->init();
	 * $userInfo = $user->getUserInfoByUid($uid);  * 
	 * [/example]
	 *
	 */
	function insert() {
		$article = new ArticleModel();
		$data = $article->create();
		dump ( $data );
		if ($data) {
			if ($article->add()) {
				echo "添加文章成功,文章编号:" . $article->getLastInsID();
			
		//$this->assign('jumpUrl',__URL__.'/index');
			//$this->success('操作成功，插入数据编号为：'.$article->getLastInsID());
			} else {
				echo "添加文章失败,错误信息:" . $article->getDbError();
			
		//$this->error('操作失败：addsection'.$article->getDbError());
			}
		} else {
			echo $article->getError();
		
		//$this->error('操作失败：数据验证( '.$article->getError().' )');
		}
	
	}
	
	/**
	 * 更新信息
	 *
	 * @param 无
	 * @return 无
	 * [example]
	 * $article = new ArticleViewModel();
	 * $user->init();
	 * $userInfo = $user->getUserInfoByUid($uid);  * 
	 * [/example]
	 *
	 */
	function update() {
		$article = new ArticleModel();
		$data = $article->create();
		dump($data);
		if($data){
			$flag = $article->save();
			if($flag){
				echo "更新信息成功!";
			} else{
				echo "更新信息失败：". $article->getDbError();
			}
		}else{
			echo "更新信息失败2：".$article->getError() . $article->getDbError();
		}
	}
}
?>